*********************************************
* Stata v14
********************************************* 

* ssc install estout, replace
  
*********************************************
* Clear memory, basic settings
********************************************* 
 	clear
	clear matrix
	clear mata
    set more off
	set scheme s1mono


*********************************************
* Bring in data
********************************************* 

cd "~/Dropbox (Samii-research)/Ethnic Quotas/data-analysis/replication-archive"
use recog-data-complete-final-2019-01-08, clear
sort cname year

********************************************* 
* Characterize conflict
********************************************* 

keep if year >= 1990
keep if violence !=.
egen ctag = tag(cname)
bysort cname: egen case = max(agreeyrbin)
tab case if ctag==1
g fatrate = 10000*(fatalities/pop) if violence ==1
bysort cname: egen avgfatrate = mean(fatrate)
sum avgfatrate if case==1 & ctag==1, detail
sum avgfatrate if case==0 & ctag==1, detail

* Check if Rwanda driving difference

sum avgfatrate if case==1 & ctag==1 & cname!=68, detail


********************************************* 
* Trends in ethnic minority rule
********************************************* 

capture log close
log using "tabs-figs/minldr-trends-log.txt", replace text
sort region
tabstat minldrnow, by(region) statistics(mean n) labelwidth(32) format(%9.2f)
tabstat minldrnow if case ==1, by(region) statistics(mean n) labelwidth(32) format(%9.2f)
sort year
lpoly minldrnow year, degree(0) jitter(2) title("") ci legend(off) bwidth(5) rec pwidth(10) ytitle("Proportion with minority ethnic rule")
reg minldrnow year, cluster(cname)
log close

keep if agreeyrbin==1

preserve

********************************************* 
* Map
********************************************* 

drop cname_s
decode cname, generate(cname_s)
replace country = cname_s if cname_s=="Serbia and Montenegro"
replace country = cname_s if cname_s=="Djibouti"
replace country = "Sudan" if cname_s=="Sudan (-2011)"
replace country = cname_s if cname_s=="Yemen"
replace country = "Pakistan" if cname_s=="Pakistan (1971-)"
g ADMIN = country
replace ADMIN = "Republic of the Congo" if country == "Congo"
replace ADMIN = "Ivory Coast" if country == "Cote d'Ivoire"
* replace ADMIN = "Republic of Serbia" if country == "Serbia and Montenegro"
* replace ADMIN = "Republic of Serbia" if country == "Yugoslavia"
drop ctag
* Tag the last year per country
bysort country: egen yrrank = rank(year), field
g ctag = (yrrank==1)
keep if ctag == 1
save "recog-data-working.dta", replace

shp2dta using "ne_110m_admin_0_countries/ne_110m_admin_0_countries", data(worlddata) coor(worldcoor) genid(id) genc(c) replace
use worlddata, clear	
merge 1:m ADMIN using "recog-data-working.dta"

* Yugoslavia and Serbia/Montenegro are not merged.  Need to hand encode:
replace ctag = 1 if ADMIN == "Republic of Serbia"
replace ctag = 1 if ADMIN == "Montenegro"
replace ctag = 1 if ADMIN == "Bosnia and Herzegovina"
replace ctag = 1 if ADMIN == "Croatia"
replace ctag = 1 if ADMIN == "Macedonia"
replace ctag = 1 if ADMIN == "Slovenia"
replace ctag = 1 if ADMIN == "South Sudan"
replace ctag = 0 if ctag == .

replace recogbin = 1 if ADMIN == "Republic of Serbia"
replace recogbin = 1 if ADMIN == "Montenegro"
replace recogbin = 1 if ADMIN == "Bosnia and Herzegovina"
replace recogbin = 1 if ADMIN == "Croatia"
replace recogbin = 1 if ADMIN == "Macedonia"
replace recogbin = 1 if ADMIN == "Slovenia"
replace recogbin = 1 if ADMIN == "South Sudan"
replace recogbin = 0 if ctag==.

g mapvar = recogbin + ctag
spmap mapvar using worldcoor.dta if ADMIN!="Antarctica", id(id) legend(off)
graph export "tabs-figs/map.pdf", replace

restore

********************************************* 
* Summary statistics
********************************************* 

capture log close 
log using "tabs-figs/data-summary.smcl", replace

egen year_bin = cut(year), at(1990 1995 2000 2005 2012) 
la define year_bin_l 1990 "1990-1995" 1995 "1996-2000" 2000 "2001-2005" 2005 "2005-2012"
la values year_bin year_bin_l
tab year_bin
tab region

tab eprethconf
tab minldrnow
tab regconcgr
egen exclnow_bin = cut(exclnow), at(0 .2 .4 .6 .8 1)
tab exclnow_bin

egen gdppc_bin = cut(gdppc), at (0 1045 4125 12736 50000)
la define gdppc_bin_l 0 "Lower income" 1045 "Lower middle income" 4125 "Upper middle income" 12736 "High income"
la values gdppc_bin gdppc_bin_l
tab gdppc_bin
tab fh_status

tab postmilvict
tab intleng
tab prevpwrshr
log close


* Table that shows bivariate relations between 
* background characteristics and recognition adoption
 
capture log close 
log using "tabs-figs/recog-patterns.smcl", replace

tab minldrnow recogbin, row nofreq
tab minldrnow recogbin, row

tab fh_status recogbin, row nofreq
tab fh_status recogbin, row

list cname year ethfrac if fh_status == 1

list cname year if fh_status == 1 & minldrnow == 1
list cname year if fh_status == 1 & minldrnow == 0

tab postmilvict recogbin, row nofreq
tab postmilvict recogbin, row

tab intleng recogbin, row nofreq
tab intleng recogbin, row

tab colhist_cname recogbin, row nofreq
tab colhist_cname recogbin, row

log close

********************************************* 
* Implementation
********************************************* 

capture drop cname_s
decode cname, g(cname_s) 
merge 1:1 cname_s year using "imp.dta", force
keep if _merge == 3
log using "tabs-figs/recog-imp-minldr.smcl", replace
tab Executive  minldrnow, col
tab Executive  minldrnow, col nofreq
tab Legislative  minldrnow, col
tab Legislative  minldrnow, col nofreq
tab Security  minldrnow, col
tab Security  minldrnow, col nofreq
tab Justice  minldrnow, col
tab Justice  minldrnow, col nofreq
tab CivilService  minldrnow, col
tab CivilService  minldrnow, col nofreq
tab Education  minldrnow, col
tab Education  minldrnow, col nofreq
tab Language  minldrnow, col
tab Language  minldrnow, col nofreq
log close


